//package net.jjjshop.front.service.shop.impl;
//
//import cn.hutool.core.util.ObjectUtil;
//import cn.hutool.core.util.StrUtil;
//import net.jjjshop.common.constants.ConfConstants;
//import net.jjjshop.common.constants.Constants;
//import net.jjjshop.common.entity.lbt.LbLineUp;
//import net.jjjshop.common.entity.order.Order;
//import net.jjjshop.common.entity.order.OrderProduct;
//import net.jjjshop.common.entity.user.User;
//import net.jjjshop.common.entity.user.UserBill;
//import net.jjjshop.common.service.lbt.LbLineUpService;
//import net.jjjshop.common.util.BigDecimalUtil;
//import net.jjjshop.common.util.DateUtil;
//import net.jjjshop.framework.common.exception.BusinessException;
//import net.jjjshop.front.service.order.OrderProductService;
//import net.jjjshop.front.service.order.OrderService;
//import net.jjjshop.front.service.order.UserBillService;
//import net.jjjshop.front.service.shop.BuyProductSettlementService;
//import net.jjjshop.front.service.user.UserService;
//import org.slf4j.Logger;
//import org.slf4j.LoggerFactory;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.scheduling.annotation.Async;
//import org.springframework.scheduling.annotation.EnableAsync;
//import org.springframework.stereotype.Service;
//
//import java.math.BigDecimal;
//import java.util.Date;
//import java.util.List;
//
//@EnableAsync
//@Service
//public class BuyProductSettlementServiceImpl implements BuyProductSettlementService {
//
//    @Autowired
//    private UserService userService;
//    @Autowired
//    private OrderService orderService;
//    @Autowired
//    private UserBillService userBillService;
//
//    @Autowired
//    private LbLineUpService lineUpService;
//
//    private static final Logger logger = LoggerFactory.getLogger(BuyProductSettlementServiceImpl.class);
//
//    @Autowired
//    private OrderProductService orderProductService;
//
//
//
//    @Override
//    public Boolean orderReceiving(Integer orderId) {
//        Order storeOrder = orderService.getById(orderId);
//        if (ObjectUtil.isNull(storeOrder)) {
//            throw new BusinessException(StrUtil.format("订单收货task处理，未找到订单，id={}", orderId));
//        }
//        List<OrderProduct> orderInfoList = orderProductService.getProductList(storeOrder.getOrderId());
//        if (null == orderInfoList || orderInfoList.isEmpty()){
//            throw new BusinessException("未找订单产品信息");
//        }
//
//        BigDecimal payPrice = storeOrder.getPayPrice();
//        Integer uid = storeOrder.getUserId();
//        User walletInfo = userService.getById(uid);
//
//        /*购买单品赠送同等价值积分*/
//        UserBill userBill = new UserBill();
//        userBill.setUid(uid);
//        userBill.setLinkId(orderId.toString());
//        userBill.setTitle("下单增加积分");
//        userBill.setNumber(payPrice);
//        userBill.setStatus(1);
//        userBill.setCreateTime(DateUtil.nowDateTime());
//        userBill.setPm(1);//增加
//        userBill.setType(Constants.USER_BILL_TYPE_INTEGRAL);
////        userBill.setBalance(userWalletInfo.getIntegral().add(payPrice));
//        userBillService.save(userBill);
//        /*购买单品赠送同等价值积分end*/
//
//
//        Integer auto = null;
//        //获取系统是否开启随机公排
////
//        BigDecimal sy = new BigDecimal("0");
//        BigDecimal dyPrice = null;
//        for (OrderProduct bean : orderInfoList){
//            //计算产品的波比收益
//            BigDecimal bob = BigDecimalUtil.mul(bean.getTotalPayPrice(),bean.getRate());
////            sy = BigDecimalUtil.addDouble(sy,BigDecimalUtil.mul(bob,new BigDecimal(bean.getPayNum())));
//        }
//
//        //查询收益中的公排订单
//        LbLineUp status1 = lineUpService.getStatus1();
//        if (null != status1){
//            BigDecimal ts = BigDecimalUtil.addDouble(status1.getIncome(),sy);
//            if (ts.doubleValue() >= status1.getTotal().doubleValue()){
//                dyPrice = BigDecimalUtil.subDouble(ts,status1.getTotal());
//                status1.setStatus(2);
//                userBillService.saveBill("公排订单收益", ConfConstants.COMMISSION_TYPE,1,BigDecimalUtil.subDouble(status1.getTotal(),status1.getIncome()) , status1.getUid(),null);
//                LbLineUp lbLineUp = lineUpService.setUpOrder();
//
//                if (null != lbLineUp){
//                    lbLineUp.setStatus(1);
//                    lbLineUp.setIncome(BigDecimalUtil.addDouble(lbLineUp.getIncome(),dyPrice));
//                    lineUpService.updateById(lbLineUp);
//                    if (dyPrice.doubleValue() > 0){
//                        //剩余金额给到下一个公排人
//                        userBillService.saveBill("公排订单收益", ConfConstants.COMMISSION_TYPE, 1, dyPrice, lbLineUp.getUid(), null);
//
//                    }
//                }
//                status1.setIncome(status1.getTotal());
//                lineUpService.updateById(status1);
//            }else{
//                status1.setIncome(ts);
//                lineUpService.updateById(status1);
//                userBillService.saveBill("公排订单收益", ConfConstants.COMMISSION_TYPE,1,sy , status1.getUid(),null);
//
//            }
//        }else{
//            //获取下一个公排收益人
//            status1 = lineUpService.setUpOrder();
//            if (null != status1) {
//                status1.setStatus(1);
//                status1.setIncome(sy);
//                lineUpService.updateById(status1);
//                userBillService.saveBill("公排订单收益", ConfConstants.COMMISSION_TYPE, 1, sy, status1.getUid(), null);
//
//            }
//        }
//        LbLineUp line = new LbLineUp();
//        line.setCreateTime(new Date());
//        line.setTotal(BigDecimalUtil.mul(storeOrder.getPayPrice(),new BigDecimal("2")));
//        line.setUid(storeOrder.getUserId());
//        line.setOrderId(storeOrder.getOrderId().toString());
//        if(!lineUpService.save(line)){
//            logger.info(storeOrder.getOrderId()+"公排异常");
//            throw new BusinessException("公排订单异常");
//
//        }
//
//        /**
//         * 确认收货送积分
//         */
//
//        String where = "paid = 1 and refund_status = 0 and uid = "+uid;
//        //首单消费送积分,用户在前5000个才送积分
//        walletInfo.setPoints(BigDecimalUtil.addDouble(new BigDecimal(walletInfo.getPoints()),payPrice).intValue());
//        walletInfo.setAllAv(BigDecimalUtil.addDouble(walletInfo.getAllAv(),payPrice));
////        walletInfo.setFirstPay(BigDecimalUtil.addDouble(userSave.getFirstPay(),payPrice));
//        userService.updateById(walletInfo);
////        }
//
//
//
//
//        //直推直接算提成
//        BigDecimal scale = new BigDecimal(0.1);//直推提成10%
//        BigDecimal price = BigDecimalUtil.mul(payPrice, scale);
//        userBillService.saveBill("直接销售奖励", ConfConstants.COMMISSION_TYPE,1, price, walletInfo.getRefereeId(),ConfConstants.YJ_FLAG);
////        }
////         marketPromotion(storeOrder);
//       rangeReward(storeOrder.getUserId(),storeOrder.getPayPrice());
//
//
//
//
//        return true;
//        /**
//         * 确认收货送积分end
//         */
//    }
//
//
//
//    @Async
//    @Override
//    public void rangeReward(Integer buyId,BigDecimal payPrice) {
//        //查询下单人
//        User buy = userService.getById(buyId);
//        userService.queryTeam(buyId,payPrice);
//        userService.queryTeam(buy.getRefereeId(),payPrice);
//        fundBalance(0,buy.getRefereeId(),payPrice);
//    }
//
//
//
//
//    /**
//     * 根据上级节点查询
//     * @param level 下级等级
//     * @param pid 付节点
//     */
//    private void fundBalance(Integer level,Integer pid,BigDecimal payPrice){
//        logger.info("pid==="+pid+"----level=="+level);
////        System.out.println("pid==="+pid+"   ----level=="+level);
//
//
//
//        User bean = userService.getById(pid);
////        TeamBean teamBean = userService.queryTeam(pid);
//
////        Integer integer = userService.selectInviteCodeNum(pid);
//        int nowLev = bean.getGradeId();
//        String price = "0.05";//
//        String str ="";
//
////
//        int a = nowLev - level;
//        if (a>0){
////            if (orderNum >1){
//            BigDecimal ac = BigDecimalUtil.mul(new BigDecimal(price),new BigDecimal(a));
////                BigDecimal b = BigDecimalUtil.divide(ac,new BigDecimal("100"));
//            userBillService.saveBill(str+"团队销售管理奖", ConfConstants.COMMISSION_TYPE,1,BigDecimalUtil.mul(payPrice,ac),pid,ConfConstants.YJ_FLAG);
////            }
//        }
//        if (nowLev < 4){
//            //不是最大的等级，还可以找上面比自己大的
//            if (a >0){
//                //当前等级大于
//                fundBalance(nowLev, bean.getRefereeId(),payPrice);
//            }else{
//                fundBalance(level, bean.getRefereeId(),payPrice);
//            }
//
//        }
//
//
//    }
//}
